﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ODS.Config;
using ODS.Utils;
using System.IO;
using ODS.Model;
using System.Windows;

namespace ODS.WebService.DeviceMonitor
{
    public class LoadAllDeviceMonitorsService
    {
        static bool IsAuthenticated = true;

        public static async Task<InfoResult> LoadAllDeviceMonitors(String imei)
        {
            String url = Constants.SERVER_URL + "/devices/" + imei + "/monitors"; ;

            InfoResult result;
            try
            {
                Stream xmlStream = await RESTfulService.HTTPGetRequest(url);

                List<DeviceMonitorModel> resultString = ParseXML(xmlStream);

                #region check authenticate
                if (!IsAuthenticated)
                {
                    InfoResult result_login = await Login_Logout.LoginService.loginService(App.userLogin.UserLoginName, App.userLogin.UserPassword);
                    if (result_login.IsSuccess)
                    {
                        if ((String)result_login.ReturnedObject == Constants.SUCCESS)
                        {
                            IsAuthenticated = true;

                            return await LoadAllDeviceMonitors(imei);
                        }
                        else
                        {
                            MessageBox.Show((String)result_login.ReturnedObject);
                            App.BackToLoginPage();

                        }
                    }
                    else
                    {
                        MessageBox.Show(result_login.Error);
                        App.BackToLoginPage();

                    }


                }
                #endregion

                result = new InfoResult(null, resultString);
            }
            catch (Exception ex)
            {
                result = new InfoResult(ex.Message, null);
            }
            return result;
        }

        private static List<DeviceMonitorModel> ParseXML(Stream xmlStream)
        {
            List<DeviceMonitorModel> listDeviceMonitorModel = new List<DeviceMonitorModel>();
            XDocument doc = XDocument.Load(xmlStream);

            var Authenticate = (from authen in doc.Descendants("unauthorized")
                                select new
                                {

                                }).FirstOrDefault();
            if (Authenticate != null)
            {
                IsAuthenticated = false;
                return null;
            }

            var deviceMonitors = from deviceMonitor in doc.Descendants("monitorDetails")
                             select new
                             {
                                 MonitorType = deviceMonitor.Element("monitorType").Value,
                                 ModifiedAt = deviceMonitor.Element("modifiedAt").Value,
                                 Processing = deviceMonitor.Element("processing").Value,
                                 Parameters = from parameter in deviceMonitor.Descendants("parameter")
                                             select new
                                             {
                                                 Name = parameter.Element("name").Value,
                                                 Value = parameter.Element("value").Value,
                                             }
                             };
            foreach (var deviceMonitor in deviceMonitors)
            {
                DeviceMonitorModel deviceMonitorModel = new DeviceMonitorModel();
                deviceMonitorModel.MonitorType = deviceMonitor.MonitorType;
                deviceMonitorModel.ModifiedAt = CommonUtil.ParseDateTimeFromString(deviceMonitor.ModifiedAt);
                deviceMonitorModel.Processing = deviceMonitor.Processing.Equals("true");
                deviceMonitorModel.ArrayParameter = new List<MonitorParameterModel>() ;
                foreach (var parameter in deviceMonitor.Parameters)
                {
                    MonitorParameterModel monitorParameterModel = new MonitorParameterModel();
                    monitorParameterModel.NameParameter = parameter.Name;
                    monitorParameterModel.Value = parameter.Value;

                    deviceMonitorModel.ArrayParameter.Add(monitorParameterModel);
                }

                listDeviceMonitorModel.Add(deviceMonitorModel);
            }


            return listDeviceMonitorModel;
        }
    }
}
